<script>
	import { verionUpdate } from 'api/common'
	export default {
		data(){
			return {
				app_info:{}
			}
		},
		onLaunch: function() {
			console.log('App Launch');
			let header = {
				version: '1.0.1',
				osType:1
			}
			verionUpdate(header).then(res=>{
				if(res.data.updateInfo.is_update == 1){
					// 更新
					uni.reLaunch({
						url:`/pages/index/index?update_type=${res.data.updateInfo.update_type}&update_url=${res.data.updateInfo.update_url}&update_title=${res.data.updateInfo.update_title}&update_content=${res.data.updateInfo.update_content}`
					})
				}
			})
			// #ifdef APP-PLUS
			this.getAppInfo().then(res=>{
				let header = {
					version: this.app_info.version,
					osType:1
				}
				verionUpdate(header).then(res=>{
					if(res.data.updateInfo.is_update == 1){
						// 更新
						uni.navigateTo({
							url:`/pages/update/update?update_type=${res.data.updateInfo.update_type}&update_url=${res.data.updateInfo.update_url}&update_title=${res.data.updateInfo.update_title}&update_content=${res.data.updateInfo.update_content}`
						})
					}
				})
			})
			// #endif
			let open_last_read = uni.getStorageSync('open_last_read');
			if(open_last_read){
				try {
				    const last_read_book = uni.getStorageSync('last_read_book');
				    if (last_read_book) {
						uni.reLaunch({
						    url: `/pages/read/read?id=${last_read_book.id}&chapter_id=${last_read_book.chapter_id}&route_type=launch`
						});
				    }
				} catch (e) {
				    // error
				}
			}
		},
		onShow: function() {
			console.log('App Show');
		},
		onHide: function() {
			console.log('App Hide');
		},
		methods:{
			getAppInfo() {
				return new Promise((resolve,reject) =>{
					plus.runtime.getProperty( plus.runtime.appid, (wgtinfo) => {
						this.app_info = wgtinfo;
						resolve('ok')
					} );
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
</style>
